package top.zywork.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.zywork.common.DateParseUtils;
import top.zywork.common.RegexUtils;
import top.zywork.enums.DatePatternEnum;
import top.zywork.enums.IncomeOutgoingTypeEnum;
import top.zywork.service.AccountStatisticsService;
import top.zywork.vo.ResponseStatusVO;
import top.zywork.vo.StatisticsDayCountVO;
import top.zywork.vo.echarts.BasicLineChartVO;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 平台出入账统计控制器类<br/>
 *
 * 创建于2019-11-19<br/>
 *
 * @author http://zywork.top 王振宇
 * @version 1.0
 */
@RestController
@RequestMapping("/account-stat")
public class AccountStatisticsController extends BaseController {

    private AccountStatisticsService accountStatisticsService;

    /**
     * 统计平台总结余，总入账，总出账
     * @return
     */
    @PostMapping("account-count")
    public ResponseStatusVO countGoodsAndOrder() {
        BigDecimal total = accountStatisticsService.countAllAccount();
        BigDecimal totalIn = accountStatisticsService.countAllAccountByType(IncomeOutgoingTypeEnum.INCOME.getValue().byteValue());
        BigDecimal totalOut = accountStatisticsService.countAllAccountByType(IncomeOutgoingTypeEnum.OUTGOING.getValue().byteValue());
        BigDecimal totalCommission = accountStatisticsService.countTotalCommission();
        return ResponseStatusVO.ok("统计平台总结余，总入账，总出账", new Object[]{total, totalIn, totalOut, totalCommission});
    }

    /**
     * 指定时间周期内的总结余数
     * @param beginDateStr
     * @param endDateStr
     * @return
     */
    @PostMapping("all-account-by-date")
    public ResponseStatusVO countAllGoodsByTime(String beginDateStr, String endDateStr) {
        if (!RegexUtils.match(RegexUtils.REGEX_DATE, beginDateStr) || !RegexUtils.match(RegexUtils.REGEX_DATE, endDateStr)) {
            return ResponseStatusVO.dataError("时间字符串格式错误，正确的格式：yyyy-MM-dd", null);
        }
        Calendar beginCalendar = DateParseUtils.parseCalendar(beginDateStr, DatePatternEnum.DATE.getValue());
        Calendar endCalendar = DateParseUtils.parseCalendar(endDateStr, DatePatternEnum.DATE.getValue());
        BigDecimal total = accountStatisticsService.countAllAccountByTime(beginCalendar.getTime(), endCalendar.getTime());
        return ResponseStatusVO.ok(beginDateStr + "至" + endDateStr + "总结余数", total);
    }

    /**
     * 指定时间周期内的每日商品数（用于echarts统计）
     * @param beginDateStr
     * @param endDateStr
     * @return
     */
    @PostMapping("goods-count-by-date")
    public ResponseStatusVO countGoodsByTime(String beginDateStr, String endDateStr) {
        if (!RegexUtils.match(RegexUtils.REGEX_DATE, beginDateStr) || !RegexUtils.match(RegexUtils.REGEX_DATE, endDateStr)) {
            return ResponseStatusVO.dataError("时间字符串格式错误，正确的格式：yyyy-MM-dd", null);
        }
        Calendar beginCalendar = DateParseUtils.parseCalendar(beginDateStr, DatePatternEnum.DATE.getValue());
        Calendar endCalendar = DateParseUtils.parseCalendar(endDateStr, DatePatternEnum.DATE.getValue());
        List<StatisticsDayCountVO> statisticsDayCountVOList = accountStatisticsService.countDayAccountByTime(beginCalendar.getTime(), endCalendar.getTime());
        BasicLineChartVO basicLineChartVO = new BasicLineChartVO();
        List<String> xAxisData = new ArrayList<>();
        List<Number> seriesData = new ArrayList<>();
        for (StatisticsDayCountVO statisticsDayCountVO : statisticsDayCountVOList) {
            xAxisData.add(statisticsDayCountVO.getDateStr());
            seriesData.add(statisticsDayCountVO.getTotalLongCount());
        }
        basicLineChartVO.setxAxisData(xAxisData);
        basicLineChartVO.setSeriesData(seriesData);
        return ResponseStatusVO.ok(beginDateStr + "至" + endDateStr + "的每日结余数", basicLineChartVO);
    }

    @Autowired
    public void setAccountStatisticsService(AccountStatisticsService accountStatisticsService) {
        this.accountStatisticsService = accountStatisticsService;
    }
}
